var g_share_panel, g_share_id, g_share_url, g_share_title, g_share_selection;

function show_share_panel(id, url, title, selection)
{
    g_share_panel = document.getElementById('sharemenu');
    document.getElementById('secondaryControls' + id).appendChild(g_share_panel);
    g_share_panel.style.display = 'block';
    g_share_id = id;
    g_share_url = url;
    g_share_title = title;
    g_share_selection = selection;
    
    document.getElementById('shareToEmail').setAttribute('href',
        'mailto:?subject=' + encodeURIComponent(g_share_title) +
        '&body=' + encodeURIComponent(
            g_share_title + "\n" +
            g_share_url + "\n\n" +
            (g_share_selection.length ? g_share_selection + "\n\n" : '') +
            "(via Instapaper)"
        )
    );
    return false;
}

function first_p_text_cb(p_text)
{
    document.getElementById('share_facebook_selection').value = p_text;
    document.getElementById('share_facebook_selection').disabled = false;
}

function share_to_facebook()
{
    g_share_panel.style.display = 'none';
    document.getElementById('share_facebook_url').value = g_share_url;
    document.getElementById('share_facebook_title').value = g_share_title;
    document.getElementById('share_facebook_title').disabled = false;
    document.getElementById('share_facebook_selection').value = g_share_selection;
    document.getElementById('share_facebook_selection').disabled = false;
    document.getElementById('share_facebook_comment').value = '';
    document.getElementById('share_facebook_comment').disabled = false;
    document.getElementById('share_to_facebook_loader').style.display = 'none';
    document.getElementById('share_to_facebook_submit_button').disabled = false;
    document.getElementById('share_to_facebook_submit_button').value = 'Post to Facebook';

    if (! g_share_selection.length) {
        document.getElementById('share_facebook_selection').disabled = true;
        document.getElementById('share_facebook_selection').value = 'Loading...';
        new Ajax('/go/' + g_share_id + '/text?first_p_text_ajax=1', { method: 'get', evalScripts: true }).request();
    }

    var s = document.getElementById('facebooksharemenu');
    document.getElementById('secondaryControls' + g_share_id).appendChild(s);
    s.style.display = 'block';
    document.getElementById('share_facebook_comment').focus();
    return false;
}

function share_to_facebook_submit()
{
    $('share_to_facebook_form').send({
		onSuccess: function(){
            document.getElementById('facebooksharemenu').style.display = 'none';
		},
		onFailure: function(){
            document.getElementById('share_facebook_title').disabled = false;    
            document.getElementById('share_facebook_selection').disabled = false;
            document.getElementById('share_facebook_comment').disabled = false;
            document.getElementById('share_to_facebook_loader').style.display = 'none';
            document.getElementById('share_to_facebook_submit_button').disabled = false;
            document.getElementById('share_to_facebook_submit_button').value = 'Post to Facebook';
            alert('Sorry, there was an error submitting your post to Facebook.');
		}
	});

    document.getElementById('share_to_facebook_loader').style.display = 'inline-block';
    document.getElementById('share_facebook_title').disabled = true;    
    document.getElementById('share_facebook_selection').disabled = true;
    document.getElementById('share_facebook_comment').disabled = true;
    document.getElementById('share_to_facebook_submit_button').disabled = true;
    document.getElementById('share_to_facebook_submit_button').value = 'Posting...';
	return false;
}

function short_url_cb(short_url)
{
    document.getElementById('share_twitter_tweet').value = g_share_title + ' ' + short_url + ' (via Instapaper)';
    document.getElementById('share_twitter_tweet').disabled = false;
    document.getElementById('share_to_twitter_submit_button').disabled = false;
    share_to_twitter_update_counter();
    document.getElementById('share_twitter_tweet').focus();
}

function share_to_twitter()
{
    g_share_panel.style.display = 'none';
    document.getElementById('share_twitter_url').value = g_share_url;
    document.getElementById('share_twitter_tweet').value = 'Shortening URL...';
    document.getElementById('share_twitter_tweet').disabled = true;
    document.getElementById('twitter_counter').innerHTML = '';
    document.getElementById('share_to_twitter_loader').style.display = 'none';
    document.getElementById('share_to_twitter_submit_button').disabled = true;
    document.getElementById('share_to_twitter_submit_button').value = 'Post to Twitter';

    new Ajax('/main/bitly_url?long_url=' + encodeURIComponent(g_share_url), { method: 'get', evalScripts: true }).request();

    var s = document.getElementById('twittersharemenu');
    document.getElementById('secondaryControls' + g_share_id).appendChild(s);
    s.style.display = 'block';
    return false;
}

function share_to_twitter_submit()
{
    if (140 - document.getElementById('share_twitter_tweet').value.length < 0) {
        alert('Sorry, Twitter posts are limited to 140 characters.');
        return false;
    }
    
    $('share_to_twitter_form').send({
		onSuccess: function(){
            document.getElementById('twittersharemenu').style.display = 'none';
		},
		onFailure: function(){
            document.getElementById('share_twitter_tweet').disabled = false;
            document.getElementById('share_to_twitter_submit_button').disabled = false;
            document.getElementById('share_to_twitter_loader').style.display = 'none';
            document.getElementById('share_to_twitter_submit_button').value = 'Post to Twitter';

            alert('Sorry, there was an error submitting your post to Twitter.');
		}
	});
	
    document.getElementById('share_to_twitter_loader').style.display = 'inline-block';
    document.getElementById('share_twitter_tweet').disabled = true;
    document.getElementById('share_to_twitter_submit_button').disabled = true;
	document.getElementById('share_to_twitter_submit_button').value = 'Posting...';
	return false;
}

function share_to_twitter_update_counter()
{
    var c, t = document.getElementById('twitter_counter');
    var remaining = 140 - document.getElementById('share_twitter_tweet').value.length;
    var noun = (remaining == -1 || remaining == 1) ? 'character' : 'characters';
    if (remaining < 0) {
        t.style.color = '#a00';
        t.innerHTML = '<b>' + (0 - remaining) + '</b> ' + noun + ' too many';
    } else {
        t.style.color = '#080';
        t.innerHTML = '<b>' + remaining + '</b> ' + noun + ' remaining';
    }    
}

function share_to_tumblr()
{
    g_share_panel.style.display = 'none';
    document.getElementById('share_tumblr_url').value = g_share_url;
    document.getElementById('share_tumblr_title').value = g_share_title;
    document.getElementById('share_tumblr_title').disabled = false;
    document.getElementById('share_tumblr_selection').value = g_share_selection;
    document.getElementById('share_tumblr_selection').disabled = false;
    document.getElementById('share_to_tumblr_loader').style.display = 'none';
    document.getElementById('share_to_tumblr_submit_button').disabled = false;
    document.getElementById('share_to_tumblr_submit_button').value = 'Post to Tumblr';

    var s = document.getElementById('tumblrsharemenu');
    document.getElementById('secondaryControls' + g_share_id).appendChild(s);
    s.style.display = 'block';
    document.getElementById('share_tumblr_title').focus();
    return false;
}

function share_to_tumblr_submit()
{
    $('share_to_tumblr_form').send({
		onSuccess: function(){
            document.getElementById('tumblrsharemenu').style.display = 'none';
		},
		onFailure: function(){
            document.getElementById('share_tumblr_title').disabled = false;    
            document.getElementById('share_tumblr_selection').disabled = false;
            document.getElementById('share_to_tumblr_loader').style.display = 'none';
            document.getElementById('share_to_tumblr_submit_button').disabled = false;
            document.getElementById('share_to_tumblr_submit_button').value = 'Post to Tumblr';
            alert('Sorry, there was an error submitting your post to Tumblr. Please check your Tumblr login in Account.');
		}
	});

    document.getElementById('share_to_tumblr_loader').style.display = 'inline-block';
    document.getElementById('share_tumblr_title').disabled = true;    
    document.getElementById('share_tumblr_selection').disabled = true;
    document.getElementById('share_to_tumblr_submit_button').disabled = true;
    document.getElementById('share_to_tumblr_submit_button').value = 'Posting...';
	return false;
}

function share_to_pinboard()
{
    g_share_panel.style.display = 'none';
    document.getElementById('share_pinboard_url').value = g_share_url;
    document.getElementById('share_pinboard_title').value = g_share_title;
    document.getElementById('share_pinboard_title').disabled = false;
    document.getElementById('share_pinboard_selection').value = g_share_selection;
    document.getElementById('share_pinboard_selection').disabled = false;
    document.getElementById('share_pinboard_tags').disabled = false;
    document.getElementById('share_to_pinboard_loader').style.display = 'none';
    document.getElementById('share_to_pinboard_submit_button').disabled = false;
    document.getElementById('share_to_pinboard_submit_button').value = 'Post to Pinboard';

    var s = document.getElementById('pinboardsharemenu');
    document.getElementById('secondaryControls' + g_share_id).appendChild(s);
    s.style.display = 'block';
    document.getElementById('share_pinboard_title').focus();
    return false;
}

function share_to_pinboard_submit()
{
    $('share_to_pinboard_form').send({
		onSuccess: function(){
            document.getElementById('pinboardsharemenu').style.display = 'none';
		},
		onFailure: function(){
            document.getElementById('share_pinboard_title').disabled = false;    
            document.getElementById('share_pinboard_selection').disabled = false;
            document.getElementById('share_to_pinboard_loader').style.display = 'none';
            document.getElementById('share_to_pinboard_submit_button').disabled = false;
            document.getElementById('share_to_pinboard_submit_button').value = 'Post to Pinboard';
            alert('Sorry, there was an error submitting your post to Pinboard. Please check your Pinboard username and password in Account.');
		}
	});

    document.getElementById('share_to_pinboard_loader').style.display = 'inline-block';
    document.getElementById('share_pinboard_title').disabled = true;    
    document.getElementById('share_pinboard_selection').disabled = true;
    document.getElementById('share_pinboard_tags').disabled = true;
    document.getElementById('share_to_pinboard_submit_button').disabled = true;
    document.getElementById('share_to_pinboard_submit_button').value = 'Posting...';
	return false;
}

function share_to_evernote()
{
    g_share_panel.style.display = 'none';
    document.getElementById('share_evernote_bookmark_id').value = g_share_id;
    document.getElementById('share_evernote_title').value = g_share_title;
    document.getElementById('share_evernote_title').disabled = false;
    document.getElementById('share_evernote_notebook').value = '';
    document.getElementById('share_evernote_notebook').disabled = false;
    document.getElementById('share_evernote_tags').value = '';
    document.getElementById('share_evernote_tags').disabled = false;
    document.getElementById('share_to_evernote_loader').style.display = 'none';
    document.getElementById('share_to_evernote_submit_button').disabled = false;
    document.getElementById('share_to_evernote_submit_button').value = 'Post to Evernote';

    var s = document.getElementById('evernotesharemenu');
    document.getElementById('secondaryControls' + g_share_id).appendChild(s);
    s.style.display = 'block';
    document.getElementById('share_evernote_title').focus();
    return false;
}

function share_to_evernote_submit()
{
    $('share_to_evernote_form').send({
		onSuccess: function(){
            document.getElementById('evernotesharemenu').style.display = 'none';
		},
		onFailure: function(){
            document.getElementById('share_evernote_title').disabled = false;    
            document.getElementById('share_evernote_notebook').disabled = false;
            document.getElementById('share_to_evernote_loader').style.display = 'none';
            document.getElementById('share_to_evernote_submit_button').disabled = false;
            document.getElementById('share_to_evernote_submit_button').value = 'Post to Evernote';
            alert('Sorry, there was an error submitting your post to Evernote. Please check your Evernote username and password in Account.');
		}
	});

    document.getElementById('share_to_evernote_loader').style.display = 'inline-block';
    document.getElementById('share_evernote_title').disabled = true;    
    document.getElementById('share_evernote_notebook').disabled = true;
    document.getElementById('share_evernote_tags').disabled = true;
    document.getElementById('share_to_evernote_submit_button').disabled = true;
    document.getElementById('share_to_evernote_submit_button').value = 'Posting...';
	return false;
}
